package com.xx.doublexchat.common.common.exception;

import cn.hutool.http.ContentType;
import com.google.common.base.Charsets;
import com.xx.doublexchat.common.common.domain.vo.response.ApiResult;
import com.xx.doublexchat.common.common.utils.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * http请求错误枚举
 */
@AllArgsConstructor
@NoArgsConstructor
public enum HttpErrorEnum {
    ACCESS_DENIED(401, "登录失效请从新登录"),
    BLACK_ERROR(401,"您已被拉黑请联系管理员")
    ;

    private Integer httpCode;
    private String desc;

    public void sendHttpError(HttpServletResponse response) throws IOException {
        response.setStatus(httpCode);
        response.setContentType(ContentType.JSON.toString(Charsets.UTF_8));
        response.getWriter().write(JsonUtils.toStr(ApiResult.fail(httpCode, desc)));
    }
}
